--
-- Author: songjianbang
-- Date: 2018-6-25 17:54:28
--

local DungeonSecretInfo = class("DungeonSecretInfo")
local Skill = require("app.public.dao.Skill")
local SkillSimpleRender = require("app.widget.renders.SkillSimpleRender")

function DungeonSecretInfo:ctor(node)
	self.secretNode = cc.CSLoader:createNode(Res.DungeonSceretNode)

	self:initDungeonNodes()
end

function DungeonSecretInfo:initDungeonNodes()
    local rootPanel = self.secretNode:getChildByName("Panel")

    self.secretNode:findChild("Panel/Text_name"):setString(L("SecretDungeon"))
    self.panelShowed = false
    self._dungeon_TouchPanel = rootPanel:getChildByName("bg01") 
    self._dungeon_TouchPanel:setTouchEnabled(true)
    self._dungeon_TouchPanel:addTouchEventListener(function(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                if self.panelShowed then
                    self:colapse()
                else
                    self:extend()
                end
            end
        end)

    self.layerLbl = self.secretNode:findChild("Panel/Button/num")

    self.name = self.secretNode:findChild("Panel/Panel_ami/Panel/tlt")

    self.skill1 = self.secretNode:findChild("Panel/Panel_ami/Panel/buff_1")
    self.skill2 = self.secretNode:findChild("Panel/Panel_ami/Panel/buff_2")
    self.skill3 = self.secretNode:findChild("Panel/Panel_ami/Panel/buff_3")

    self.rule1 = self.secretNode:findChild("Panel/Panel_ami/Panel_time/medal_3")
    self.rule2 = self.secretNode:findChild("Panel/Panel_ami/Panel_time/medal_2")
    self.rule3 = self.secretNode:findChild("Panel/Panel_ami/Panel_time/medal_1")

    self.battleTime = self.secretNode:findChild("Panel/Panel_ami/Panel_time/Text_count")
    self.bossProgress = self.secretNode:findChild("Panel/Panel_ami/Panel_time/boss")
    self.monsterProgress = self.secretNode:findChild("Panel/Panel_ami/Panel_time/foe")

    self.aniPanel = self.secretNode:findChild("Panel/Panel_ami")

    self:refreshUI()
    self.secretNode:schedule(handler(self, self.updateDungeonUi), 0.5)
end

function DungeonSecretInfo:refreshUI()
    if not self.secretNode:isVisible() then
        return
    end

    local info = SecretManager.info

    if not info then
        return
    end

    local teamModel = app:getInst("TeamModel")
    local layerNum = checknumber(info.layer)
    if teamModel then
        local t = teamModel:getTeam()
        if t then
            layerNum = t:getLeaderLayer()
        end
    end
    self.layerLbl:setString(tostring(layerNum))

    local mapId = MapManager:getMapId()
    local conf = GD:queryMapById(mapId)
    self.name:setString(L(conf.string))

    self.skillRenders = {}
    local bufids = {}--info.npcStartBuffIds
    local teamModel = app:getInst("TeamModel")
    if teamModel then
        local t = teamModel:getTeam()
        if t then
            bufids = t:getNpcStartBuffIds()
        end
    end
    
    for i, v in ipairs(bufids) do
        local skill = Skill.new()
        skill:read({uid=v})
        skill.getIcon = function(self)
            if not self.icon then
                self.icon = GD:getImagePath(Res.buffIconPath, self.db.icon)
            end
            return self.icon
        end

        self.skillRenders[i] = SkillSimpleRender.createWithData(skill, self["skill"..tostring(i)])
        self.skillRenders[i]:setTipEnable(true)
    end

    local grades = SecretManager:getGradesFromLayer(info.layer)
    if grades then
        for i = 1, 3 do 
            self["rule"..tostring(i)]:setString(grades[i].title..": "..grades[i].hmstr)
        end
    end
end

function DungeonSecretInfo:showDungeonUi()
    -- UIHandler:getDesktopController():getDesktop():getTeamGroup():getMemberRoot():hide()
    -- UIHandler:getDesktopController():getDesktop():getTeamGroup():getFunctionRoot():hide()
    self.secretNode:show()
    self:extend()
    self:refreshUI()
end

function DungeonSecretInfo:colapse()
    local tl = self.secretNode:getTimeline()
    tl:play("out", false)
    tl:setLastFrameCallFunc(function()
        self.aniPanel:hide()
        tl:clearLastFrameCallFunc()
    end)

    self.panelShowed = false
end

function DungeonSecretInfo:extend()
    local tl = self.secretNode:getTimeline()
    self.aniPanel:show()
    tl:play("in", false)

    self.panelShowed = true
end

function DungeonSecretInfo:hideDungeonUi()
    -- UIHandler:getDesktopController():getDesktop():getTeamGroup():getMemberRoot():show()
    -- UIHandler:getDesktopController():getDesktop():getTeamGroup():getFunctionRoot():show()
    self.secretNode:hide()
end

function DungeonSecretInfo:updateDungeonUi()
    if not self.secretNode:isVisible() then
        return
    end

    local time = DungeonManager:getTotalTime()

    local timeStr = string.format(L("lua_code_text_472"), tostring(time))
    local bossStr = string.format(L("lua_code_text_335"), (DungeonManager:getDungeonLeaderProgress() or ""))
    local monsterStr = string.format(L("lua_code_text_434"), (DungeonManager:getDungeonMonsterProgress() or ""))
    self.battleTime:setString(timeStr)
    self.bossProgress:setString(bossStr)
    self.monsterProgress:setString(monsterStr)
end

return DungeonSecretInfo
